package com.kerrykidz.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kerrykidz.system.dao.mapper.GblSecBranEntityMapper;
import com.kerrykidz.system.dao.mapper.SecRole2usrEntityMapper;
import com.kerrykidz.system.dao.mapper.SecUsrEntityMapper;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.dao.model.SecRole2usrEntity;
import com.kerrykidz.system.dao.model.SecUsrEntity;
import com.kerrykidz.system.dao.model.SysAuditLogEntity;
import com.kerrykidz.system.service.ISysAuditLogService;
import com.kerrykidz.system.service.IUserService;
import com.kerrykidz.system.util.ConstantUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.util.security.PrincipalInfo;
import com.kerrykidz.system.vo.UserInfo;
import com.rdp.framework.exception.RDPException;

/**
 * 用户Service层
 * 
 * @author zhuss
 * 
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {

	@Autowired
	SecUsrEntityMapper usrMapper;

	@Autowired
	SecRole2usrEntityMapper secRole2usrEntityMapper;

	@Autowired
	ISysAuditLogService isysAuditLogService;

	@Autowired
	private GblSecBranEntityMapper branEntityMapper;

	/**
	 * 用户登录
	 */
	public PrincipalInfo checkLoginUser(String username, String password) throws RDPException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("loginId", username);
		params.put("loginPwd", password);
		SecUsrEntity secu = usrMapper.findUserByNameAndPass(username, password);
		GblSecBranEntity bran = this.branEntityMapper.selectBranByAbbrCode(secu.getAbbrCode());
		if (secu != null) {
			// 记录日志
			recordLog(secu);
			PrincipalInfo principal = new PrincipalInfo(secu);
			principal.setContAddr(bran.getContAddr());
			return principal;
		}
		return null;
	}

	/**
	 * 记录登录用户的历史
	 * 
	 * @param secu
	 */
	public void recordLog(SecUsrEntity secu) throws RDPException {
		SysAuditLogEntity sal = new SysAuditLogEntity();
		sal.setAuditTime(new Date());
		sal.setAuditType(ConstantUtil.AUDIT_TYPE_LOGIN);
		sal.setLogInfo(secu.getUsrName() + ConstantUtil.AUDIT_TYPE_LOGINFO);
		sal.setFkBranId(secu.getFkBranId());
		sal.setModiBy(secu.getPkUsrId());
		sal.setModiTime(new Date());
		isysAuditLogService.saveSysAuditLogEntity(sal);
	}

	/**
	 * 根据用户ID查找用户
	 */
	@Override
	public SecUsrEntity findEntityById(String userID) throws RDPException {
		return usrMapper.findEntityById(userID);
	}

	/**
	 * 根据用户ID获取该用户的所有角色
	 */
	@Override
	public List<String> findAllRoleByUserId(String userId)
			throws RDPException {
		List<SecRole2usrEntity> list = secRole2usrEntityMapper.findAllRoleByUserId(userId);
		List<String> strlist = new ArrayList<String>();
		if (null != list && list.size() != 0) {
			for (SecRole2usrEntity sr : list) {
				strlist.add(sr.getFkRoleId());
			}
		}
		return strlist;
	}

	/**
	 * 修改密码
	 */
	@Override
	public void updatePwd(String newLoginPwd, String userId) throws RDPException {
		usrMapper.updatePwdById(newLoginPwd, userId);
	}

	/**
	 * 根据 角色id，获取所有相关角色的用户列表
	 * 
	 * @Description List<SecUsrEntity>
	 * @author chenyu 2014年8月22日 下午2:17:55
	 */
	public List<SecUsrEntity> listUserByRoleId(String roleId) {
		UserInfo user = UserUtil.getUserFromSession();
		String fkBrandId = user.getBranId();
		if (user.getAbbrCode().equals("ZB")) {
			fkBrandId = "";
		}
		return usrMapper.listUserByRoleId(roleId, fkBrandId);
	}

	public List<SecUsrEntity> selectUsesJobInfo(SecUsrEntity entity)
			throws RDPException {
		return usrMapper.selectUsesJobInfo(entity);
	}

}
